//
//  ViewController.swift
//  transform3D
//
//  Created by Tsunami on 2017/8/11.
//  Copyright © 2017年 Tsunami. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    //本地的旧项目直接推送到远程服务器
    
    // 1.git init
    // 2.git add .
    // 3.git commit -am "提交的信息自己输入"
    
    // 4.建一个远程的空仓库GitHub或码云 复制仓库地址
    // 5.git remote add origin git@xx.xx.xx.xx:repos/xxx/xxx/xxx.git(地址)
    // 6.git push origin master push到仓库这时会报错 说Updates were rejected because the tip of your current branch is behind
    
    // 7. 因为他们是两个不同的项目，要把两个不同的项目合并，git需要添加一句代码，在git pull，这句代码是在git 2.9.2版本发生的，最新的版本需要添加--allow-unrelated-histories 假如我们的源是origin，分支是master，那么我们 需要这样写git pull origin master --allow-unrelated-histories需要知道，我们的源可以是本地的路径
    // 7.1 git pull origin master --allow-unrelated-histories
    // 8. 在重复 3 6 操作

    
    let diceView = UIView()
    var angle = CGPoint()
    
    override func viewDidLoad() {
        self.angle = CGPoint(x: 0, y: 0)
        super.viewDidLoad()
        
        addDice()
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(viewTranform(sender:)))
        
        diceView.addGestureRecognizer(pan)
        
    }

    func addDice() {
        let viewFrame = UIScreen.main.bounds
        
        var diceTransform = CATransform3DIdentity
        
        
        diceView.frame = CGRect(x: 0, y: viewFrame.maxY/2-50, width: viewFrame.width, height: 100)
        
        let dice1 = UIImageView.init(image: UIImage(named: "dice1.jpg"))
        dice1.frame = CGRect(x: viewFrame.maxX/2-50, y: 0, width: 100, height: 100)
        diceTransform = CATransform3DTranslate(diceTransform, 0, 0, 50)
        dice1.layer.transform = diceTransform
        
        let dice6 = UIImageView.init(image: UIImage(named: "dice6.jpg"))
        dice6.frame = CGRect(x: viewFrame.maxX/2-50, y: 0, width: 100, height: 100)
        diceTransform = CATransform3DTranslate(CATransform3DIdentity, 0, 0, -50)
        dice6.layer.transform = diceTransform
        
        let dice2 = UIImageView.init(image: UIImage(named: "dice2.jpg"))
        dice2.frame = CGRect(x: viewFrame.maxX/2-50, y: 0, width: 100, height: 100)
        diceTransform = CATransform3DRotate(CATransform3DIdentity, CGFloat.pi / 2, 0, 1, 0)
        diceTransform = CATransform3DTranslate(diceTransform, 0, 0, 50)
        dice2.layer.transform = diceTransform
        
        let dice5 = UIImageView.init(image: UIImage(named: "dice5.jpg"))
        dice5.frame = CGRect(x: viewFrame.maxX/2-50, y: 0, width: 100, height: 100)
        diceTransform = CATransform3DRotate(CATransform3DIdentity, CGFloat.pi / 2, 0, 1, 0)
        diceTransform = CATransform3DTranslate(diceTransform, 0, 0, -50)
        dice5.layer.transform = diceTransform

        
        let dice3 = UIImageView.init(image: UIImage(named: "dice3.jpg"))
        dice3.frame = CGRect(x: viewFrame.maxX/2-50, y: 0, width: 100, height: 100)
        
        diceTransform = CATransform3DRotate(CATransform3DIdentity, (-CGFloat.pi / 2), 1, 0, 0)
        diceTransform = CATransform3DTranslate(diceTransform, 0, 0, 50)
        dice3.layer.transform = diceTransform
        
        let dice4 = UIImageView.init(image: UIImage(named: "dice3.jpg"))
        dice4.frame = CGRect(x: viewFrame.maxX/2-50, y: 0, width: 100, height: 100)
        
        diceTransform = CATransform3DRotate(CATransform3DIdentity, (-CGFloat.pi / 2), 1, 0, 0)
        diceTransform = CATransform3DTranslate(diceTransform, 0, 0, -50)
        dice4.layer.transform = diceTransform
        
        
        diceView.addSubview(dice1)
        diceView.addSubview(dice2)
        diceView.addSubview(dice3)
        diceView.addSubview(dice4)
        diceView.addSubview(dice5)
        diceView.addSubview(dice6)

        
        view.addSubview(diceView)
    }
    
    func viewTranform(sender: UIPanGestureRecognizer) {
        
        let point = sender.translation(in: diceView)
        
        let angleX = angle.x + (point.x/30)
        let angleY = angle.y - (point.y/30)
        
        var tranform = CATransform3DIdentity
        
        tranform.m34 = -1/500
        
        tranform = CATransform3DRotate(tranform, angleX, 0, 1, 0)
        tranform = CATransform3DRotate(tranform, angleY, 1, 0, 0)
        diceView.layer.sublayerTransform = tranform
        
        if sender.state == .ended {
        
            angle.x = angleX
            angle.y = angleY
        }
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

